﻿@namespace MudBlazor.UnitTests.TestComponents

<MudSelect Label="Select Item" 
    MultiSelection="true" 
    T="@TestItem" 
    SelectedValues="@SelectedItems" 
    ToStringFunc="@ToString"
    MultiSelectionTextFunc="GetMultiSelectionText">

    @foreach (TestItem item in AllItems)
    {
        <MudSelectItem Value="@item" />
    }
</MudSelect>

@code {

    private List<TestItem> AllItems = new();    
    private HashSet<TestItem> SelectedItems = new();
    protected override async Task OnInitializedAsync()
    {
        AllItems.Add(new TestItem { A = "FirstA" });
        AllItems.Add(new TestItem { A = "SecondA" });
        AllItems.Add(new TestItem { A = "ThirdA" });
        
        SelectedItems.Add(AllItems[0]);
        SelectedItems.Add(AllItems[1]);
        await base.OnInitializedAsync();
    }

    private string ToString(TestItem x)
        => x is null ? string.Empty : $"{x.A}";

    private string GetMultiSelectionText(List<string> selectedValues)
    {
        return $"Selected values: {string.Join(", ", selectedValues.Select(x => x))}";
    }

    private class TestItem
    {
        public string A { get; set; }        
    }
}